package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import pojo.Categories;
import pojo.Food;
import pojo.Merchant;
import service.CategoriesService;
import service.FoodService;
import util.FileUtil;

@Controller
public class FoodController {

	@Resource(name="foodServiceImpl")
	private FoodService foodService;
	
	@Resource(name = "categoriesServiceImpl")
    private CategoriesService categoriesService;
    @Resource
    Environment env;
	
	@ResponseBody
	@RequestMapping(value="/showFoodList",method = RequestMethod.GET)
	public List<Food> getFoodList(HttpServletRequest request,String typeId){
		HttpSession session = request.getSession();
		Merchant merchant = (Merchant) session.getAttribute("user");
		String mId = merchant.getId();
		//String mId = "8a5e9d1a5dd547c4015dd547c6ee0000" 测试用;
		List<Food> foodList = new ArrayList<Food>();
		String tId = (typeId == null ? "all" : typeId);
	
		foodList = foodService.findFood(tId,mId);
		
		return foodList;
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteFood",method = RequestMethod.DELETE)
	public Food deleteFood(String id){
		System.out.println("deletefood");
		return foodService.delete(id);
	}
	 
	@RequestMapping(value="/addFood",method = RequestMethod.POST)
	public String addFood(Food food, HttpServletRequest request,MultipartFile foodImage,String categoryId){
    
		String foodImagePath = new Date().getTime() + food.getName();
		Merchant merchant = (Merchant) request.getSession().getAttribute("user");
		String readPath = env.getProperty("savePath") + "/" + foodImagePath;
		 try {
			FileUtil.uploadWithOutBase64(foodImage.getInputStream(), readPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		 Categories categories = categoriesService.load(categoryId);
		 
		 food.setCategories(categories);
		 food.setMerchant(merchant);
		 categories.getFoodList().add(food);
		 merchant.getFoodList().add(food);
		 foodService.add(categories,merchant);
		 return " redirect:/showFoodList";
	}
	
	@RequestMapping(value="/showEditFood",method = RequestMethod.GET)
	public String showEditFood(String id,Model model){
		  Food food = foodService.load(id);
		  List<Categories> categoriesList = categoriesService.findAll();
		  model.addAttribute("food",food);
		  model.addAttribute("categories", categoriesList);
		  return "/WEB-INF/sec/control/EditFood";
	}
	
	@RequestMapping(value="/editFood",method = RequestMethod.POST)
	public String editFood(Food food, HttpServletRequest request,MultipartFile foodImage,String categoryId){
    
		String foodImagePath = new Date().getTime() + food.getName();
		Merchant merchant = (Merchant) request.getSession().getAttribute("user");
		String readPath = env.getProperty("savePath") + "/" + foodImagePath;
		 try {
			FileUtil.uploadWithOutBase64(foodImage.getInputStream(), readPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		 Categories categories = categoriesService.load(categoryId);
		 food.setCategories(categories);
		 food.setMerchant(merchant);
		 foodService.update(food);
		 return " redirect:/showFoodList";
	}
	
}
